﻿/// <summary>
/// FileName: TokenReader.cs
/// Author: Jiang Xiaolong
/// Created Time: 2015.05.12
/// Version: 1.0
/// Company: Sunnytech
/// Function: 分词缓存读取器，通过此类读取单个字符缓存；
///
/// Changed By:
/// Modification Time:
/// Discription:
/// </summary>
using UnityEngine;
using System.Collections;
using Expression.api;

namespace Expression.impl
{
	public class TokenReader : ForwardQuery<ExpChar>
	{
		private string _inputExp = "";
		private int _line = 1;
		private int _pos = 0;
		private int _count = 0;
		private int _operator = 0;

		public TokenReader(string input_exp)
		{
			if (input_exp == "")
			{
				//TODO
				//problemCollector.Add(ParseError.Warning(new PositionLocate()))
			}
			this._inputExp = input_exp;
			this._count = input_exp.Length;
			this._operator = 0;
		}

		protected override ExpChar EndOfInput()
		{
			return new ExpChar('\0', _line, _pos);
		}

		protected override ExpChar Fetch()
		{
			if (_operator <= _count - 1)
			{
				char readChar = _inputExp[_operator];
				_operator++;
				if (readChar == '\n')
				{
					_line++;
					_pos = 0;
				}
				_pos++;
				return new ExpChar(readChar, _line, _pos);
			}
			else
			{
				//报错
				return null;
			}
		}

		public override string ToString()
		{
			if (ItemBuffer.Count == 0)
			{
				return _line + ":" + _pos + ": Buffer empty";
			}
			if (ItemBuffer.Count < 2)
			{
				return _line + ":" + _pos + ": " + current();
			}
			return _line + ":" + _pos + ": " + current() + ", " + next();
		}
	}
}

